package com.wunu.zn.api.appliance.service;

import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wunu.zn.api.appliance.model.Machine;
import com.wunu.zn.api.appliance.model.MachineRecord;

import common.AppConst;
import common.plugin.mybatis.Db;
import common.utils.http.HttpClientConnectionManager;
import net.sf.json.JSONObject;

@Transactional
@Service
public class ActivityMqService {
	private static Logger log = Logger.getLogger(ActivityMqService.class);
	public static DefaultHttpClient httpclient;
	static
	{
		httpclient = new DefaultHttpClient();
		httpclient = (DefaultHttpClient)HttpClientConnectionManager.getSSLInstance(httpclient);
	}
	/**
	 * 处理MQ接受到的信息
	 * @param message
	 */
	public JSONObject  getDevMessage(String message){
		 JSONObject json = JSONObject.fromObject(message);
		 String param = json.getString("param");
		 JSONObject jsonParam = JSONObject.fromObject(param);
		return jsonParam;
	}
	/**
	 * 处理MQ接受到的信息
	 * @param message
	 */
	public int  messageListener( JSONObject json){
		 Machine machine = new Machine();
		 MachineRecord mr = new MachineRecord();
		 int code = 0;
		 String[] the = json.get("messages").toString().split(",");
		 if(the.length>0){// deviceId
		 String deviceId = json.get("name").toString();
		 String model = "";
		 Integer state = null;
		 String data = "";
		 if(the[0]!=null&&!"".equals(the[0])){
			 model = the[0];
		 }
		 if(the[1]!=null&&!"".equals(the[1])){
			 if(the[0].equals("0000")){
				 state = 0;
			 }else{
				 state = 1;
			 }
		 }
		 if(the[2]!=null&&!"".equals(the[2])){
			 data = the[2];
		 }		 
		 if(Machine.getMachine(deviceId)==null){
			 machine.set("deviceId", deviceId);
			 machine.set("model",model); 
			 machine.set("createTime", new Date());
			 Db.save(machine);
		 }
		 try {
			 mr.set("deviceId", deviceId);
			 mr.set("model",model); 
			 mr.set("state", state);
			 mr.set("data", data);
			 mr.set("createTime", new Date());
			 code = Db.save(mr);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("ActivityMqService->>messageListener:name->"+12+";messages->"+mr.toString()+";createTime->"+new Date());
			}
		}
		return code;
	}
	
	/**
	 * Http发送数据到网关
	 * @param message
	 * @param cname
	 * @return
	 */
	public  String sendMessage(String message,String cname){
		DefaultHttpClient client = new DefaultHttpClient();
	  	  client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
	  	  HttpPost httpost= HttpClientConnectionManager.getPostMethodToNetty(AppConst.MQ_URL,cname,"s2p");
	       try {
	  		 httpost.setEntity(new StringEntity(message, "UTF-8"));
	  		 HttpResponse response = httpclient.execute(httpost);
	  	     String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
	  	   return JSONObject.fromObject(jsonStr).get("code").toString();
	  	} catch (Exception e) {
	  	   e.printStackTrace();
	  	   log.error("ActivityMqService->>messageListener:name->"+12+";messages->"+message+";createTime->"+new Date());
	  	   return "0";
	  	}
	}

}
